// pages/msubmit_info/msubmit_info.js
var encryp = require('../../assets/js/encryp.js')
var wxRequest = require('../../assets/js/wxRequest.js')
var common = require('../../assets/js/common.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true,
    revicemodalKey: true,
    revicemodalLock: true,
    token: '',
    randomKey: '',
    sign: '',
    userId: '',
    authInfo:[],
    keys: [],
    locks:[],
    authOrRevice: true,   // 授权/领用单
    keyOrLock: true,   // 钥匙/锁具领用
    buttonShow: '0',   //操作按钮的显示规则
  },
  /**
   * 根据审批单号获取钥匙
   */
  getKeyByNo: function (allowNum) {
    var that = this;
    var simpleObject = {
      id: allowNum
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    var url = getApp().globalData.httpUrl + '/reviceNote/getkeyByNo';
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {
        console.log(res.data)
        that.setData({
          keys: res.data
        })
      })
  },
  /**
   * 根据审批单号获取锁具
   */
  getLockByNo: function (allowNum) {
    var that = this;
    var simpleObject = {
      id: allowNum
    };
    var param = common.packParam(that.data.randomKey, simpleObject);
    var url = getApp().globalData.httpUrl + '/reviceNote/getLockByNo';
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {
        console.log(res.data)
        that.setData({
          locks: res.data
        })
      })
  },
  openAlertKey: function () {
    var that = this;
    this.setData({
      revicemodalKey: !this.data.revicemodalKey
    })
    console.log('钥匙详情')
    that.getKeyByNo(that.data.authInfo.allowNum)

  },
  openAlertLock: function () {
    var that = this;
    this.setData({
      revicemodalLock: !this.data.revicemodalLock
    })
    console.log('锁具详情')
    that.getLockByNo(that.data.authInfo.allowNum)
  },
  openAlert: function () {
    this.setData({
      hiddenmodalput: !this.data.hiddenmodalput
    })
  },
  //取消按钮
  cancel: function () {
    this.setData({
      hiddenmodalput: true ,revicemodalKey: true,
      revicemodalLock: true});
    console.log('用户点击取消')
  },
  //确认    
  confirm: function () {
    this.setData({
      hiddenmodalput: true, revicemodalKey: true,
      revicemodalLock: true})
    console.log('用户点击确认')
  },
  /**
   * 同意/打回审批
   */
  updateNote: function (e){
    var that = this;
    var index = parseInt(e.currentTarget.dataset.state)
    var allowNum = that.data.authInfo.allowNum
    var simpleObject = {
      id: allowNum,
      state: index
    };
    var param = common.packParam(that.data.randomKey, simpleObject);

    if (that.data.authOrRevice){  //授权
      var url = getApp().globalData.httpUrl + '/authNote/updateAuthNote';
    }else{             //领用
      var url = getApp().globalData.httpUrl + '/reviceNote/updateReceive';
    }
    var info = wxRequest.postRequest(url, param, that.data.token).
      then(res => {
        console.log(res.data)
        var index = parseInt(res.data)
        if (index=='1'){
          
          wx.reLaunch({
            url: '/pages/msg_success/msg_success?msgInfo=' + 1  //信息提示页只返回主页
          })
        } else if (index == '2'){
          wx.reLaunch({
            url: '/pages/msg_success/msg_success?msgInfo=' + 1  //信息提示页只返回主页
          })
        }else{
          wx.showToast({
            title: '操作失败',
            icon: 'none',
            duration: 3000
          });
        }
      })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 每个页面都需要先获取缓存中的token和security
    var tokens = wx.getStorageSync('token')
    var randomKey = wx.getStorageSync('randomKey')
    var sign = wx.getStorageSync('sign')
    var id = wx.getStorageSync('id')
    if (tokens && randomKey) {
      that.setData({
        token: tokens,
        randomKey: randomKey,
        sign: sign,
        userId: id
      })
    }
    // ----------------------------
    let info;
    if (options.authInfo){
      that.setData({
        authOrRevice: true
      })
      info = JSON.parse(options.authInfo);

      var lock = info.lockId;
      var lockArr = lock.split(",");
      that.setData({
        locks: lockArr
      })

    } else if (options.reviceInfo){
      that.setData({
        authOrRevice: false
      })
      info = JSON.parse(options.reviceInfo);
      if (info.type == 1) {  //钥匙领用
        that.setData({
          keyOrLock: true,
        })
      } else {             //锁具领用
        that.setData({
          keyOrLock: false,
        })
      }
    }
    var state = parseInt(info.state)
    that.setData({
      buttonShow: state,
      authInfo: info
    })
    console.log(info)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})